layui.use(['form', 'table', 'laydate', 'admin', 'ax', 'func', 'formSelects'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var laydate = layui.laydate;

    /**
     * 班级管理
     */
    var Team = {
        tableId: "teamTable"
    };

    /**
     * 初始化表格的列
     */
    Team.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'teamTitle', sort: true, title: '班级号'},
            {field: 'source', sort: true, title: '班级类型'},
            {field: 'typeName', sort: true, title: '专业'},
            {field: 'status', sort: true, title: '状态'},
            {field: 'studentsCount', sort: true, title: '在班学生数量',templet(d){
                return d.studentsCount + '人';
                }},
            {field: 'lecturerName', sort: true, title: '讲师'},
            {field: 'teacherName', sort: true, title: '班主任'},
            {field: 'createTime', sort: true, title: '开班日期'},
            {field: 'endTime', sort: true, title: '计划结课日期'},
            {field: 'lastEndTime', sort: true, title: '最终结课日期'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    getSpecialtyType();
    showTeachers();

    //获取专业类型并展示
    function getSpecialtyType() {
        var ajax = new $ax(Feng.ctxPath + "/specialtyType/showList");
        var result = ajax.start();
        $('#specialtyId').append('<option value="">所学专业</option>');
        result.data.map(item =>{
            $('#specialtyId').append('<option value="'+item.id+'">'+item.typeName+'</option>');
        });
        form.render('select');
    }

    //获取讲师及班主任
    function showTeachers(){
        var ajax = new $ax(Feng.ctxPath + "/team/showTeachers");
        var result = ajax.start();
        $('#lecturerId').append('<option value="">请选择授课讲师</option><optgroup label="专业高级阶段讲师">');
        result.data.zhuanyegaoji.map(item =>{
            $('#lecturerId').append('<option value="'+item.userId+'">'+item.name+'</option>');
        });
        $('#lecturerId').append('</optgroup><optgroup label="项目实训就业阶段讲师">');
        result.data.shixun.map(item =>{
            $('#lecturerId').append('<option value="'+item.userId+'">'+item.name+'</option>');
        });
        $('#lecturerId').append('</optgroup>');
        $('#teacherId').append('<option value="">请选择班主任</option>');
        result.data.banzhuren.map(item =>{
            $('#teacherId').append('<option value="'+item.userId+'">'+item.name+'</option>');
        });
        form.render('select');
    }

    /**
     * 点击查询按钮
     */
    Team.search = function () {
        var queryData = {
            "teamTitle" : $("#teamTitle").val(),
            "source" : $("#source").val(),
            "status" : $("#status").val(),
            "specialtyId" : $("#specialtyId").val(),
            "lecturerId" : $("#lecturerId").val(),
            "teacherId" : $("#teacherId").val(),
        };
        table.reload(Team.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Team.openAddDlg = function () {
        func.open({
            title: '添加班级',
            content: Feng.ctxPath + '/team/add',
            tableId: Team.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      Team.openEditDlg = function (data) {
          func.open({
              title: '修改班级',
              content: Feng.ctxPath + '/team/edit?id=' + data.id,
              tableId: Team.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    Team.exportExcel = function () {
        var checkRows = table.checkStatus(Team.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Team.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/team/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Team.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Team.tableId,
        url: Feng.ctxPath + '/team/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Team.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Team.search();
    });
    // 重置按钮点击事件
    $('#reset').click(function () {
        $("#teamTitle").val('');
        $("#source").val('');
        $("#status").val('');
        $("#specialtyId").val('');
        $("#lecturerId").val('');
        $("#teacherId").val('');
        form.render('select');
        Team.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    Team.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        Team.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Team.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Team.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Team.onDeleteItem(data);
        }
    });
});
